<template>
  <Dialog v-model="dialogVisible" :appendToBody="true" :scroll="true" title="订单明细选择" width="60%">
    <ContentWrap>
      <!-- 搜索工作栏 -->
      <el-form
          class="-mb-15px"
          :model="queryParams"
          ref="queryFormRef"
          :inline="true"
          label-width="68px"
      >
        <el-form-item label="订单号" prop="displayPoNumber">
          <el-input
            v-model="queryParams.displayPoNumber"
            placeholder="请输入采购订单号"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="采购员" prop="agentName">
          <el-input
            v-model="queryParams.agentName"
            placeholder="请输入采购员"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>

        <el-form-item label="物料号" prop="itemCode">
          <el-input
            v-model="queryParams.itemCode"
            placeholder="请输入物料号"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="物料名称" prop="itemDescription">
          <el-input
            v-model="queryParams.itemDescription"
            placeholder="请输入物料名称"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="项目名称" prop="projectName">
          <el-input
            v-model="queryParams.projectName"
            placeholder="请输入项目名称"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>

        <el-form-item>
          <el-button @click="handleQuery">
            <Icon icon="ep:search" class="mr-5px"/>
            搜索
          </el-button>
          <el-button @click="resetQuery">
            <Icon icon="ep:refresh" class="mr-5px"/>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>
    <!-- 列表 -->
    <ContentWrap>
      <el-table v-loading="loading" :data="list" :stripe="true"
                :show-overflow-tooltip="true"
                ref="multipleTableRef"
                @selection-change="handleSelectionChange">

        <el-table-column type="selection" :selectable="selectable" width="55"/>

        <el-table-column label="序号" width="90" align="center" fixed="left" prop="rank">
          <template #default="scope">
            {{ (queryParams.pageNo - 1) * queryParams.pageSize + scope.$index + 1 }}
          </template>
        </el-table-column>

        <el-table-column label="长客订单号" align="center" prop="displayPoNumber"  width="150px" />
        <el-table-column label="订单总金额" align="center" prop="sumAmount"  width="150px" />
        <el-table-column label="采购员" align="center" prop="agentName" />
        <el-table-column label="物料号" align="center" prop="itemCode" width="150px" />
        <el-table-column label="物料名称" align="center" prop="itemDescription" width="150px"  />
        <el-table-column label="数量" align="center" prop="quantity" />
        <el-table-column label="单位" align="center" prop="unitMeasLookupCode" />
        <el-table-column label="单价" align="center" prop="unitPrice" />
        <el-table-column label="每" align="center" prop="eachNumber" />
        <el-table-column label="项目代码" align="center" prop="projectCode" />
        <el-table-column label="项目名称" align="center" prop="projectName" />
        <el-table-column label="状态" align="center" prop="statusDesc" />
      </el-table>
      <!-- 分页 -->
      <Pagination
          :total="total"
          v-model:page="queryParams.pageNo"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
      />
    </ContentWrap>

    <template #footer>
      <el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>


<script setup lang="ts">
import {defaultProps, handleTree} from '@/utils/tree'
import { PurOrderApi, PurOrderVO } from '@/api/ck/purorder'
import {ElTable} from "element-plus";


/** 订单明细 列表 */
defineOptions({name: 'OrderDetailForm'})

const message = useMessage() // 消息弹窗
const {t} = useI18n() // 国际化

const multipleTableRef = ref<InstanceType<typeof ElTable>>()
const multipleSelection = ref<any[]>([])


const loading = ref(true) // 列表的加载中
const list = ref<PurOrderVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  displayPoNumber: undefined,
  agentName: undefined,
  releaseDate: [],
  typeLookupDesc: undefined,
  itemCode: undefined,
  itemDescription: undefined,
  projectName: undefined,
  zbtzsbh: undefined,
  deliveryStatus: undefined,
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('订单明细选择') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中
const handleSelectionChange = (val: any[]) => {
  multipleSelection.value = val
}

const submitForm = () => {
  formLoading.value = true
  if (multipleSelection.value?.length === 0) {
    message.warning('请先选择相关采购订单行项目后操作！')
    return
  }

  try {
    emits('onGetChild', multipleSelection.value) // 返回选择的原始数据由使用方处理
  } finally {
    formLoading.value = false
    // 关闭弹窗
    dialogVisible.value = false
  }
}



// 定义事件，更新父组件
const emits = defineEmits<{
  'update:modelValue': [node: SimpleFlowNode | undefined]
  'find:parentNode': [nodeList: SimpleFlowNode[], nodeType: number]
  'find:recursiveFindParentNode': [
    nodeList: SimpleFlowNode[],
    curentNode: SimpleFlowNode,
    nodeType: number
  ]
}>()


/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await PurOrderApi.getPurOrderPage2(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}


/** 打开弹窗 */
const open = async (getListFunc: Function) => {
  dialogVisible.value = true
  await nextTick()
  if (multipleSelection.value.length > 0) {
    multipleTableRef.value!.clearSelection()
  }
  await getList(getListFunc)
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗




/** 初始化 **/
onMounted(() => {
  getList()
})




</script>
